<template>
  <div class="rightbox" v-cloak>
    <h1>参数新增</h1>
    <div class="add-box" style="width:450px;">
      <el-form ref="form" :model="form" label-width="110px">
        <el-form-item
          prop="paramindex"
          label="选择一级类别"
          :rules="[
            { required: true, message: '请选择所属类别', trigger: 'blur' }
          ]"
        >
          <el-select v-model="form.paramindex"  placeholder="请选择所属类别">
            <el-option
              v-for="item in form.paramList"
              :key="item.paramList"
              :label="item.name"
              :value="item.paramindex">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item
          prop="name"
          label="参数名称"
          :rules="[
            { required: true, message: '请填写参数名称', trigger: 'blur' }
          ]"
        >
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item
          label="状态"
          prop="state"
        >
          <el-radio v-model="form.state" label="1">启用</el-radio>
          <el-radio v-model="form.state" label="-1">禁用</el-radio>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submit('form')">提交</el-button>
          <el-button  @click="resetForm('form')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data () {
    return {
      form: {
        name: '',
        paramindex: '2',
        paramList: []
      },
      paramindex: ''
    }
  },
  methods: {
    getData: function () {
      var that = this
      that.$axios.request('post', '/Param/Index', {
        superior: 0
      })
        .then((res) => {
          if (res.code === 0) {
            that.form.paramList = res.data
            that.getParams()
          }
        })
    },
    getParams () {
      var that = this
      that.paramindex = that.$route.params.paramindex
      if (that.paramindex) {
        that.$axios.request('post', '/Param/Eidt', {
          paramindex: that.paramindex
        })
          .then((res) => {
            if (res.code === 0) {
              that.form.name = res.data.name
              that.form.paramindex = res.data.superior
              that.form.state = res.data.state + ''
            }
          })
      } else {
        that.$router.push({
          name: 'paramList'
        })
      }
    },
    submit: function (form) {
      var that = this
      that.$refs[form].validate((valid) => {
        if (valid) {
          that.$axios.request('post', '/Param/EidtPost', {
            paramindex: that.paramindex,
            superior: that.form.paramindex,
            name: that.form.name,
            state: that.form.state
          })
            .then((res) => {
              if (res.code === 0) {
                that.$axios.alert('修改成功', '提示', function () {
                  that.$router.push({
                    name: 'paramList'
                  })
                })
              }
            })
        } else {
          return false
        }
      })
    },
    resetForm: function (form) {
      this.$refs[form].resetFields()
    }
  },
  mounted () {
    this.getData()
  }
}
</script>
